package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    //获取已登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){


        try {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }

    }

    //添加新的用户
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user,Integer[] roleIds){

        try {
            userService.addNewUser(user,roleIds);
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult pageResult = userService.findPage(queryPageBean);
//            System.out.println(pageResult);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //第一次删除,可能有子关联
    @RequestMapping("/deleteFirst")
    public Result deleteFirst(Integer id){

        try {
            Result result = userService.deleteFirst(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }

    }


    //第二次删除,包括子关联
    @RequestMapping("/deleteSecond")
    public Result deleteSecond(Integer id){

        try {
            userService.deleteSecond(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
    }


    //根据id查询用户信息
    @RequestMapping("/findById")
    public Result findById(Integer id){

        try {
            com.itheima.pojo.User user = userService.findById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    //根据用户id查询关联角色id集合
    @RequestMapping("/findRoleIds")
    public Result findRoleIds(Integer id){

        try {
            List<Integer> roleIds = userService.findRoleIds(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //修改密码
    @RequestMapping("/editUser")
    public Result editUser(@RequestBody com.itheima.pojo.User user,Integer[] roleIds){

        try {
            userService.editUser(user,roleIds);
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
    }

}
